﻿using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using XCore.Extensions;

namespace SaaS.Core
{
    public static class CoreSetting
    {
        static CoreSetting()
        {
            Configuration = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json")
               .Build();
        }
        public static IConfiguration Configuration;
        public const string Error_ID = "错误编号";
        /// <summary>
        /// 错误日志消息队列Key
        /// </summary>
        public const string SYSTEM_ERROR = "SYSTEM_ERROR";
        public const string SYSTEM_TRACE = "SYSTEM_TRACE";
        /// <summary>
        /// 客户端日志记录key
        /// </summary>
        public const string CLIENTLOG_KEY = "CLIENTLOG";
        /// <summary>
        /// token全局变量
        /// </summary>
        public const string Token = "Authorization";
        /// <summary>
        /// 错误日志通知Key
        /// </summary>
        public const string System_Error_Hub_Key = "ErrorNotice";
        /// <summary>
        /// 日志消息队列库 索引
        /// </summary>
        public const int LOG_INDEX = 5;
        public const string DefaultPassword = "a123456";
        public const string DefaultAdminName = "admin";
        public static string GetUrl(this string url)
        {
            if (url.IsNullOrEmpty()) return string.Empty;
            return UtilServer.Url + url;
        }
        public static class UtilServer
        {
            public static string Url
            {
                get
                {
                    return Configuration["Util:Url"];
                }
            }
            public static string ApiKey
            {
                get
                {
                    return Configuration["Util:ApiKey"];
                }
            }
            /// <summary>
            /// 请求头
            /// </summary>
            public static IDictionary<string, string> Header = new Dictionary<string, string> { { "ApiKey", ApiKey } };
        }

        /// <summary>
        /// 推送服务地址
        /// </summary>
        public static class HubServer
        {
            public static string Url
            {
                get
                {
                    return Configuration["Hub:Url"];
                }
            }
            public static string ApiKey
            {
                get
                {
                    return Configuration["Hub:ApiKey"];
                }
            }
            /// <summary>
            /// 请求头
            /// </summary>
            public static IDictionary<string, string> Header = new Dictionary<string, string> { { "ApiKey", ApiKey } };
        }
        /// <summary>
        /// 订阅者
        /// </summary>
        public static string Audience
        {
            get { return Configuration["JWTToken:Audience"]; }
        }
        /// <summary>
        /// 发起人
        /// </summary>
        public static string Issuer
        {
            get { return Configuration["JWTToken:Issuer"]; }
        }
        /// <summary>
        /// 过期时间 毫秒
        /// </summary>
        public static long Expire
        {
            get { long.TryParse(Configuration["JWTToken:Expire"], out long result); return result; }
        }
        /// <summary>
        /// 默认数据库连接
        /// </summary>
        public static string DefaultConnectionString
        {
            get
            {
                return Configuration.GetConnectionString("Default");
            }
        }
        /// <summary>
        /// 日志数据库连接
        /// </summary>
        public static string LogConnectionString
        {
            get
            {
                return Configuration.GetConnectionString("Log");
            }
        }
        /// <summary>
        /// Redis默认连接对象
        /// </summary>
        public static string RedisConnectionString
        {
            get
            {
                return Configuration.GetConnectionString("Redis");
            }
        }
    }
}
